<template>
	<view class="modal" :class="[showModal ? 'modal-show':'']">
		<view class="update-user-wrap" :class="[showModal ? 'update-user-show':'']">
			<view class="title-wrap">
				<text>修改个人信息</text>
				<view class="close" @tap='close'>
					<uni-icons type="closeempty" size="20"></uni-icons>
				</view>
			</view>
			<view class="tip line-v-center">需要完善头像昵称后才能购买商品哦</view>
			<view class="update-info">
				<view class="update-item">
					<label>头像</label>
					<view class="input-con">
						<button open-type="chooseAvatar" hover-class="none" @chooseavatar="chooseavatar"
							class='avatar-btn'>
							<view class="avatar-no">
								<image src="/static/user/user-default.png" class='no-avatar-icon' mode="aspectFill" v-if='!avatarUrl'>
								</image>
								<image class="avatar" :src="avatarUrl" mode="" v-else></image>
								<image src='/static/user/camera.png' class='camera'></image>
							</view>
						</button>
					</view>
				</view>
				<view class="update-item">
					<label>昵称</label>
					<view class="input-con">
						<input type='nickname' v-model="nickName" placeholder="请输入昵称" placeholder-style="color:#e1e1e1" @blur="getVal" />
					</view>
				</view>
			</view>
			<button class="save line-center" :disabled="isDisabled" :loading="isDisabled" @tap="saveUserInfo">保存</button>
		</view>
	</view>
</template>

<script>
	const app = getApp()
	export default {
		name: "improve-user",
		props: {
			showModal: Boolean
		},
		data() {
			return {
				avatarUrl: '',
				nickName:'',
				isDisabled:false,
			};
		},
		methods: {
			chooseavatar(e) {
				let that = this;
				console.log('e', e)
				that.getImgUrl(e.detail.avatarUrl)
			},
			getImgUrl(path) {
				let that = this;
				uni.showLoading({
					title: '上传中...',
					mask: true
				})
				console.log('path',path)
				uni.uploadFile({
					url: `${app.globalData.hostUrl}/${app.getApi().baseApi.upload}`,
					filePath: path,
					name: 'iFile',
					formData: {
						type: 10
					},
					header: {
						'Authorization': uni.getStorageSync('TOKEN') ? uni.getStorageSync('TOKEN') : ""
					},
					success: (uploadFileRes) => {
						var res = JSON.parse(uploadFileRes.data)
						uni.showLoading({
							title: '上传成功',
							mask: true
						})
						uni.hideLoading()
						if (res.code == 0) {
							that.avatarUrl = res.file_url
						}
					}
				});
			},
			getVal(e) {
				console.log('e',e)
				this.nickName = this.$trim(e.detail.value);
				console.log('this.nickName',this.nickName)
			},
			close() {
				this.$emit('close')
			},
			saveUserInfo(){
				let that = this;
				if (!this.avatarUrl) {
					uni.showToast({
						title: '头像不可为空',
						icon: 'none'
					})
					return
				}
				if (!this.nickName) {
					uni.showToast({
						title: '昵称不可为空',
						icon: 'none'
					})
					return
				}
				that.isDisabled = true;
				app.globalRequest({
					api: app.getApi().loginApi.saveUserData,
					data: {
						avatar: that.avatarUrl,
						nickname: that.nickName
					},
					method: 'post',
				}).then(res => {
					if (res.code == 0) {
						console.log(res)
						uni.showToast({
							title: '授权成功',
							icon: 'none'
						})
						that.$emit('close')
						that.isDisabled = false;
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						that.isDisabled = false;
					}
				}).catch(err => {
							
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.modal {
		width: 100%;
		height: 100%;
		background: rgba(0, 0, 0, 0.3);
		position: fixed;
		left: 0;
		top: 0;
		z-index: 222;
		opacity: 0;
		visibility: hidden;
		transition: all 0.1s ease-in-out;
	}

	.modal-show {
		opacity: 1;
		visibility: visible;
	}

	.update-user-wrap {
		width: 100%;
		height: 600rpx;
		background: #FFFFFF;
		border-radius: 24rpx 24rpx 0rpx 0rpx;
		position: fixed;
		left: 0;
		bottom: 0;
		font-size: 26rpx;
		visibility: hidden;
		transform: translate3d(0, 100%, 0);
		transform-origin: center;
		transition: all 0.3s ease-in-out;
	}

	.update-user-show {
		visibility: visible;
		transform: translate3d(0, 0, 0);
	}

	.title-wrap {
		display: flex;
		justify-content: space-between;
		padding: 42rpx 32rpx 30rpx;
		color: #666;
	}

	.tip {
		width: 100%;
		height: 76rpx;
		background: #FAFAFA;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		color: #999999;
		padding: 0 32rpx;
		box-sizing: border-box;
	}


	.update-item {
		height: 100rpx;
		border-bottom: 2rpx solid #FAFAFA;
		display: flex;
		align-items: center;
		padding: 0 32rpx;

		.input-con {
			flex: 1;
			height: 100%;
			display: flex;
			justify-content: flex-end;

			input {
				width: 100%;
				height: 100%;
				font-size: 28rpx;
				text-align: right;
			}
		}

		.avatar-btn {
			width: 84rpx;
			height: 84rpx;
			margin-left: inherit;
			margin-right: inherit;
			margin-top:10rpx;
			.avatar-no {
				width: 100%;
				height: 100%;
				border-radius: 100%;
				position: relative;
				image{
					width: 100%;
					height: 100%;
					border-radius: 100%;
				}
				.no-avatar-icon {
					width: 100%;
					height: 100%;
					border-radius: 100%;
				}

				.camera {
					width: 24rpx;
					height: 24rpx;
					position: absolute;
					right: 0rpx;
					bottom: 0rpx;
				}
			}
		}
	}

	.save {
		width: 692rpx;
		height: 96rpx;
		background: #07C160;
		border-radius: 10rpx;
		color: #fff;
		font-size: 32rpx;
		margin: 82rpx auto 0;
	}
</style>